package com.humanres.activity;

import android.app.TimePickerDialog;
import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciko.code.base.BaseActicity;
import com.ciko.code.bean.User;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.Req.AddOverTimeFlowReq;
import com.ciko.code.http.Req.GetNextPersonReq;
import com.ciko.code.type.EmployeeEnum;
import com.ciko.code.utils.Util;
import com.mywork.R;
import com.squareup.picasso.Picasso;
import com.ui.dialog.DataTimePicker;


/**
 * Created by 12154 on 2015/11/10.
 */
public class ResOverWorkActivity
        extends
        BaseActicity
{
	
	private RelativeLayout	 rectchooseType;
	private RelativeLayout	 rectchooseTimeStart;
	private RelativeLayout	 rectchooseTimeEnd;
							 
	private TextView		 tv_startTimeLeave;
	private TextView		 tv_endTimeLeave;
	private TextView		 tv_dayCountLeave;
	private TextView		 tv_Retroactive;
	private TextView		 tv_Job;
	private TextView		 tv_Name;
	private ImageView		 ivHanderIcon;
							 
	private EditText		 etresonLeave;
							 
	private Button			 btnsubmitLeave;
	                         //    int year;
	                         //    int month;
	                         //    int day;
	                         //    int hour;
	                         //    int minute;
	                         
	//    private DatePickerDialog startTimeDialog;
	private TimePickerDialog startTime1Dialog;
	//        private TimeDateDialog startTimeDialog;
	//    private long startTime = 0;
	//    private long endTime = 0;
	//
	//    private TimeDateDialog endTimeDialog;
	private DataTimePicker	 startTimeDialog;
							 
	private long			 startTime = 0;
	private long			 endTime   = 0;
									   
	private DataTimePicker	 endTimeDialog;
	private TimePickerDialog endTime1Dialog;
							 
	private double			 dayCount  = 0;
									   
	private String			 tye;
	private String			 reson	   = "";
	private TextView		 tvTypeOverWork;
	User					 nextUser  = null;
									   
	@Override
	protected void initTopBar() {
		setTitle("加班申请");
		enableNavLeftImg();
	}
	
	@Override
	protected void initLayout() {
		setContentView(R.layout.activity_res_over_work);
	}
	
	@Override
	protected void initView() {
		btnsubmitLeave = (Button) findViewById(R.id.btn_submitLeave);
		tv_startTimeLeave = (TextView) findViewById(R.id.tv_startTimeLeave);
		tv_endTimeLeave = (TextView) findViewById(R.id.tv_endTimeLeave);
		tv_dayCountLeave = (TextView) findViewById(R.id.tv_dayCountLeave);
		etresonLeave = (EditText) findViewById(R.id.et_resonLeave);
		tvTypeOverWork = (TextView) findViewById(R.id.tvTypeOverWork);
		rectchooseTimeEnd = (RelativeLayout) findViewById(R.id.rect_chooseTimeEnd);
		rectchooseTimeStart = (RelativeLayout) findViewById(R.id.rect_chooseTimeStart);
		rectchooseType = (RelativeLayout) findViewById(R.id.rect_chooseType);
		//        tv_Retroactive = (TextView) findViewById(R.id.tv_Retroactive);
		ivHanderIcon = (ImageView) findViewById(R.id.ivHanderIcon);
		tv_Job = (TextView) findViewById(R.id.tvJob);
		tv_Name = (TextView) findViewById(R.id.tvName);
	}
	
	@Override
	protected void initAdapter() {
	
	}
	
	@Override
	protected void initDates() {
		
		startTimeDialog = new DataTimePicker(ResOverWorkActivity.this, listener);
		endTimeDialog = new DataTimePicker(ResOverWorkActivity.this, endlistener);
		//        final Calendar calendar = Calendar.getInstance(Locale.CHINA);
		//        startTimeDialog=new TimeDateDialog(this, new TimeDateDialog.OnDateTimeClickListener() {
		//            @Override
		//            public void yearDay(int year1, int month1, int day1,int hour1, int minute1) {
		//                tv_startTimeLeave.setText(year1 + "-" + month1 + "-" + day1 + " " + hour1 + ":"+minute1);
		//                startTime=DateUtil.StringToDate(year1 + "-" + month1 + "-" + day1 + " " + hour1 + ":" + minute1 + ":00",4).getTime();
		//
		//                if (endTime != 0 && startTime != 0) {
		//
		//                    int count = (int) ((endTime - startTime) % ( 60 * 60 * 1000));
		//                    int count1 = (int) ((endTime - startTime) / ( 60 * 60 * 1000));
		//
		//                    tv_dayCountLeave.setText(count1 + (count==0?0:0.5) +"小时");
		//                    dayCount=(count1 + (count==0?0:0.5))/24;
		//                }
		//            }
		//        });
		//
		//        endTimeDialog = new TimeDateDialog(this, new TimeDateDialog.OnDateTimeClickListener() {
		//            @Override
		//            public void yearDay(int year1, int month1, int day1,int hour1, int minute1) {
		//                tv_endTimeLeave.setText(year1 + "-" + month1 + "-" + day1 + " " + hour1 + ":"+minute1);
		//                endTime=DateUtil.StringToDate(year1 + "-" + month1 + "-" + day1 + " " + hour1 + ":" + minute1 + ":00",4).getTime();
		//                if (endTime != 0 && startTime != 0) {
		//                    int count = (int) ((endTime - startTime) % ( 60 * 60 * 1000));
		//                    int count1 = (int) ((endTime - startTime) / ( 60 * 60 * 1000));
		//
		//                    tv_dayCountLeave.setText(count1 + (count==0?0:0.5) +"小时");
		//                    dayCount=(count1 + (count==0?0:0.5))/24;
		//                }
		//            }
		//        });
		
		//        DateUtil.StringToDate(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":00",4);
		//        startTimeDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
		//            @Override
		//            public void onDateSet(final DatePicker view, final int year, final int monthOfYear, final int dayOfMonth) {
		//
		//                startTime1Dialog=new TimePickerDialog(ResOverWorkActivity.this, new TimePickerDialog.OnTimeSetListener() {
		//                    @Override
		//                    public void onTimeSet(TimePicker timePicker, int i, int i1) {
		//                        tv_startTimeLeave.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth + " " + i + ":"+i1);
		//                        startTime = view.getCalendarView().getDate();
		//                        long nowTime = System.currentTimeMillis();
		////                        if (startTime>nowTime) {
		////                            tv_Retroactive.setText("否");
		////                        }
		////                        else {
		////                            tv_Retroactive.setText("是");
		////                        }
		//                        endTimeDialog.getDatePicker().setMinDate(startTime);
		//
		//                        if (endTime != 0 && startTime != 0) {
		//
		//                            dayCount = (int) ((endTime - startTime) / (24 * 60 * 60 * 1000));
		//
		//                            tv_dayCountLeave.setText(dayCount + "");
		//                        }
		//                    }
		//                }, calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE),true);
		//
		//                startTime1Dialog.show();
		//
		//            }
		//        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
		
		//        endTimeDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
		//            @Override
		//            public void onDateSet(final DatePicker view, final int year, final int monthOfYear, final int dayOfMonth) {
		//                endTime1Dialog=new TimePickerDialog(ResOverWorkActivity.this, new TimePickerDialog.OnTimeSetListener() {
		//                    @Override
		//                    public void onTimeSet(TimePicker timePicker, int i, int i1) {
		//                        tv_endTimeLeave.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth + " " + i + ":"+i1);
		//                        endTime = view.getCalendarView().getDate();
		//
		//                        if (endTime != 0 && startTime != 0) {
		//                            dayCount = (int) ((endTime - startTime) / (24 * 60 * 60 * 1000));
		//                            tv_dayCountLeave.setText(dayCount + "");
		//                        }
		//                    }
		//                }, calendar.get(Calendar.HOUR), calendar.get(Calendar.MINUTE),true);
		//                endTime1Dialog.show();
		//            }
		//        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
		
		//获取审批人
		doRequestHttp(new GetNextPersonReq());
	}
	
	@Override
	protected void initListener() {
		rectchooseTimeStart.setOnClickListener(this);
		rectchooseTimeEnd.setOnClickListener(this);
		
		btnsubmitLeave.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		
		if (v == btnsubmitLeave)
		{
			if (check())
			{
				progressDialog.show();
				doRequestHttp(new AddOverTimeFlowReq(tye, startTime, endTime, dayCount, reson, "", nextUser.getId(), nextUser.getStaffName()));
			}
		}
		else if (v == rectchooseTimeEnd)
		{
			endTimeDialog.show();
		}
		else if (v == rectchooseTimeStart)
		{
			startTimeDialog.show();
		}
		else if (v == rectchooseType)
		{
		
		}
		
		//        switch (v.getId()) {
		//            case R.id.btn_submitLeave:
		//
		//                if (check()) {
		//                    doRequestHttp(new AddOverTimeFlowReq(tye, startTime, endTime, dayCount, reson, "", App.getUser().getId(), App.getUser().getStaffName()));
		//                }
		//
		//                break;
		//            case R.id.rect_chooseTimeEnd:
		//                endTimeDialog.show();
		//                break;
		//            case R.id.rect_chooseTimeStart:
		//                startTimeDialog.show();
		//                break;
		//            case R.id.rect_chooseType:
		//
		//                break;
		//        }
	}
	
	private boolean check() {
		
		tye = tvTypeOverWork.getText().toString().trim();
		reson = etresonLeave.getText().toString().trim();
		
		if (TextUtils.isEmpty(tye))
		{
			showToast("请选择加班类型");
			return false;
		}
		else if (startTime == 0)
		{
			showToast("请选择开始时间");
			return false;
		}
		else if (endTime == 0)
		{
			showToast("请选择结束时间");
			return false;
		}
		else if (dayCount <= 0)
		{
			showToast("结束时间必须大于开始时间");
			return false;
		}
		else if (TextUtils.isEmpty(reson))
		{
			showToast("请填写加班事由");
			return false;
		}
		else if (reson.length() > 50)
		{
			showToast("字数不能超过50字");
			return false;
		}
		else if (null == nextUser)
		{
			showToast("请选择审批人");
			return false;
		}
		
		return true;
	}
	
	@Override
	public void onResponseSuccess(BaseResponse<?> response, String tag) {
		super.onResponseSuccess(response, tag);
		switch (tag) {
			case "AddOverTimeFlowReq":
				progressDialog.dismiss();
				showToast("提交成功");
				Intent intent = new Intent();
				setResult(1001, intent);
				finish();
				break;
			case "GetNextPersonReq":
				nextUser = (User) response.getReturnObject();
				if (null != nextUser)
				{
					tv_Job.setText(EmployeeEnum.typeOfValue(nextUser.getUserPositionMark()));
					tv_Name.setText(nextUser.getStaffName());
					if (!TextUtils.isEmpty(nextUser.getUserHead()))
					                                                Picasso.with(this).load(nextUser.getUserHead()).into(ivHanderIcon);
					if (nextUser.getUserPositionMark() == 9999) {
						Util.showToast(this, R.string.params_less_tip);
					}
				}
				break;
		}
	}
	
	DataTimePicker.OnDateTimeSetListener listener	 = new DataTimePicker.OnDateTimeSetListener()
	                                                 {
		                                                 @Override
		                                                 public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth, int hour, int mintue) {
			                                                 String curDate = "" + year + "-" + getTimeString(monthOfYear + 1) + "-" + getTimeString(dayOfMonth) + " " + getTimeString(hour) + ":" + getTimeString(mintue);
			                                                 tv_startTimeLeave.setText(curDate);
			                                                 tv_startTimeLeave.setTextColor(Color.BLACK);
			                                                 startTime = Util.getStringtoDate(curDate).getTime();
															 
			                                                 dipatchTime(startTime, endTime);
		                                                 }
	                                                 };
													 
	DataTimePicker.OnDateTimeSetListener endlistener = new DataTimePicker.OnDateTimeSetListener()
	                                                 {
		                                                 @Override
		                                                 public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth, int hour, int mintue) {
			                                                 String curDate = "" + year + "-" + getTimeString(monthOfYear + 1) + "-" + getTimeString(dayOfMonth) + " " + getTimeString(hour) + ":" + getTimeString(mintue);
			                                                 tv_endTimeLeave.setText(curDate);
			                                                 tv_endTimeLeave.setTextColor(Color.BLACK);
			                                                 endTime = Util.getStringtoDate(curDate).getTime();
			                                                 dipatchTime(startTime, endTime);
		                                                 }
	                                                 };
													 
	private String getTimeString(int time) {
		return (time < 10 ? "0" : "") + time;
	}
	
	private void dipatchTime(long startTime, long endTime) {
		if (endTime != 0 && startTime != 0)
		{
			long mss = endTime - startTime;
			int hour = (int) (mss / (60 * 60 * 1000));
			int minutes = (int) (mss % (60 * 60 * 1000));
			
			dayCount = (hour + (minutes == 0 ? 0 : 1));
			
			System.out.println("hour:" + dayCount);
			tv_dayCountLeave.setText(Util.getDayAndHour((int) dayCount));
		}
	}
	
	@Override
	public void onResponseFail(String tag, int resultCode) {
		super.onResponseFail(tag, resultCode);
		switch (tag) {
			case "AddOverTimeFlowReq":
				progressDialog.dismiss();
                Util.showToast(this, "提交失败，请稍后再试");
				break;
		}
	}
	
	/**
	 * @param mss
	 *            要转换的毫秒数
	 * @return 该毫秒数转换为 * days * hours * minutes * seconds 后的格式
	 * @author fy.zhang
	 */
	public static String formatDuring(long mss) {
		long days = mss / (1000 * 60 * 60 * 24);
		long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
		long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
		long seconds = (mss % (1000 * 60)) / 1000;
		return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds ";
	}
	
}
